package com.xln.furns.web;

import com.xln.furns.entity.*;
import com.xln.furns.service.OrderService;
import com.xln.furns.service.impl.OrderServiceimpl;
import com.xln.furns.utils.DateUtils;
import com.xln.furns.utils.JDBCUtilsByDruid;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "OrderServlet", value = "/OrderServlet")
public class OrderServlet extends BasicServlet {
    OrderService orderService = new OrderServiceimpl();
    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cart cart =(Cart) request.getSession().getAttribute("cart");
        Member member = (Member) request.getSession().getAttribute("member");
        if (cart == null || cart.getItems().isEmpty()){
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }
        if (member == null){
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
            return;
        }

//        String orderId = null;
//        try {
//            orderId = orderService.saveOrder(cart,member.getId());
//            JDBCUtilsByDruid.commit();
//        } catch (Exception e) {
//            JDBCUtilsByDruid.rollback();
//            e.printStackTrace();
//        }

        String orderId = orderService.saveOrder(cart,member.getId());

        request.getSession().setAttribute("orderId",orderId);
        response.sendRedirect(request.getContextPath()+"/views/order/checkout.jsp");
    }


    protected void cartList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = DateUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DateUtils.parseInt(req.getParameter("pageSize"), 4);
        Member member = (Member) req.getSession().getAttribute("member");
        if (member == null){
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
            return;
        }

        OrderService orderService = new OrderServiceimpl();
        Page<Order> orderPage = orderService.queryOrderList(pageNo, pageSize, member.getId());
        StringBuilder url = new StringBuilder("OrderServlet?action=cartList");
        orderPage.setUrl(url.toString());
        req.setAttribute("orderPage",orderPage);
        req.getRequestDispatcher("/views/order/order.jsp").forward(req,resp);

    }
    protected void cartItemList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = DateUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DateUtils.parseInt(req.getParameter("pageSize"), 4);
        String orderId = req.getParameter("orderId");
        Member member = (Member) req.getSession().getAttribute("member");
        if (member == null){
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
            return;
        }

        OrderService orderService = new OrderServiceimpl();
        Page<OrderItem> orderItemPage = orderService.queryOrderItemList(pageNo, pageSize, orderId);
        StringBuilder url = new StringBuilder("OrderServlet?action=cartItemList");
        orderItemPage.setUrl(url.toString());
        req.setAttribute("orderItemPage",orderItemPage);
        req.setAttribute("orderId",orderId);
        Integer orderItemTotalCount = orderService.queryOrderItemTotalCount(orderId);
        req.setAttribute("orderItemTotalCount",orderItemTotalCount);
        String totalPrice = req.getParameter("totalPrice");
        req.setAttribute("totalPrice",totalPrice);
        req.getRequestDispatcher("/views/order/order_detail.jsp").forward(req,resp);

    }

}
